package manage

import (
	"lingc/models"
	"lingc/services"
	"lingc/utils" // 导入 common 包
	"strings"

	"github.com/gin-gonic/gin"
)

// PageSysLang 获取所有Banner
func PageSysLang(c *gin.Context) {

	/*
		Keywords string `json:"keywords" form:"keywords"`
		Status   string `json:"status" form:"status"`
	*/
	var dataRequest struct {
		Keywords string `json:"keywords" form:"keywords"`
		Status   string `json:"status" form:"status"`
		Page     int    `json:"page" form:"page"`
		PageSize int    `json:"pageSize" form:"pageSize"` // Exported field
	}
	// log.Printf("PageUser: %+v", dataRequest)

	if err := c.ShouldBind(&dataRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	where := ""
	if dataRequest.Keywords != "" {
		keyword := "'%" + dataRequest.Keywords + "%'"
		where += " (`title` like " + keyword + " or `name` like " + keyword + ")"
	}
	if dataRequest.Status != "" {
		if where != "" {
			where += " and "
		}
		where += " status = " + dataRequest.Status
	}

	list, page, total, err := services.NewSysLangService(utils.MysqlClient).PageSysLangList(where, dataRequest.Page, dataRequest.PageSize)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"list": list, "page": page, "total": total})
}

// GetUser 获取Banner
func GetSysLang(c *gin.Context) {

	var dataRequest struct {
		ID string `json:"id" form:"id"`
	}
	where := ""
	if err := c.ShouldBind(&dataRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if dataRequest.ID != "" {
		if where != "" {
			where += " and "
		}
		where += " id = " + dataRequest.ID
	}
	data, err := services.NewSysLangService(utils.MysqlClient).GetSysLang(where)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// CreateUser 创建Banner
func CreateSysLang(c *gin.Context) {

	/**
	Title     string `json:"title"`
	Name      string `json:"name"`
	Code      string `json:"code"`
	LangCode  string `json:"lang_code"`
	IsDefault uint8  `json:"is_default"`
	Status    uint8  `json:"status"`
	AppID     int    `json:"app_id"`
	*/
	var createRequest struct {
		Title     string `json:"title" form:"title"`
		Name      string `json:"name" form:"name"`
		Code      string `json:"code" form:"code"`
		LangCode  string `json:"lang_code" form:"lang_code"`
		IsDefault uint8  `json:"is_default" form:"is_default"`
		Status    uint8  `json:"status" form:"status"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	data := &models.SysLangCreate{
		Title:     createRequest.Title,
		Name:      createRequest.Name,
		Code:      createRequest.Code,
		LangCode:  createRequest.LangCode,
		IsDefault: createRequest.IsDefault,
		Status:    createRequest.Status,
	}
	data, err := services.NewSysLangService(utils.MysqlClient).CreateSysLang(data)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// UpdateUser 更新Banner
func UpdateSysLang(c *gin.Context) {
	var createRequest struct {
		ID        uint   `json:"id" form:"id"`
		Title     string `json:"title" form:"title"`
		Name      string `json:"name" form:"name"`
		Code      string `json:"code" form:"code"`
		LangCode  string `json:"lang_code" form:"lang_code"`
		IsDefault uint8  `json:"is_default" form:"is_default"`
		Status    uint8  `json:"status" form:"status"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	data := &models.SysLangUpdate{
		ID:        createRequest.ID,
		Title:     createRequest.Title,
		Name:      createRequest.Name,
		Code:      createRequest.Code,
		LangCode:  createRequest.LangCode,
		IsDefault: createRequest.IsDefault,
		Status:    createRequest.Status,
	}
	data, err := services.NewSysLangService(utils.MysqlClient).UpdateSysLang(data)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// DeleteUser 删除Banner
func DeleteSysLang(c *gin.Context) {
	var createRequest struct {
		ID  uint   `json:"id" form:"id"`
		IDs string `json:"ids" form:"ids"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if createRequest.ID == 0 && len(createRequest.IDs) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if createRequest.ID != 0 {
		if err := services.NewSysLangService(utils.MysqlClient).DeleteSysLang(createRequest.ID); err != nil {
			utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
			return
		}
	}
	if len(createRequest.IDs) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	idStrs := strings.Split(createRequest.IDs, ",")
	var ids []string
	for _, idStr := range idStrs {
		idStr = strings.TrimSpace(idStr)
		if idStr == "" {
			continue
		}
		ids = append(ids, idStr)
	}
	// Convert IDs slice to a format suitable for batch deletion
	if len(ids) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "No valid IDs provided"})
		return
	}
	if err := services.NewSysLangService(utils.MysqlClient).DeleteSysLangs(ids); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	// ... 这里添加删除Banner的逻辑 ...
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"msg": "Delete data"})
}
